package net.itemzone.framework.webservice.cxf.client;

import java.util.HashMap;
import java.util.Map;

import net.itemzone.framework.exception.SurfilterException;

import org.apache.cxf.frontend.ClientProxyFactoryBean;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company: 深圳任子行网络技术有限公司
 * </p>
 * 
 * @version 1.00 Apr 10, 2009
 * @author zhaoyuntao 可根据客户端ID返回对应的service Modified History:
 * 
 */
public class MuitiClientFactory<T> {
	private Map<String, T> clientMap = new HashMap<String, T>();
	private String serviceClass;
	private WsdlProvider wsdlProvider;
	private HttpClientAuthentiacationHandler httpClientAuthentiacationHandler;

	public synchronized T getClientById(String clientId) {
		if (clientMap.containsKey(clientId)) {
			return clientMap.get(clientId);
		}
		ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
		String address = wsdlProvider.getWsdlAddressByClientId(clientId);
		factory.setUsername(httpClientAuthentiacationHandler
				.getDefaultUserName());
		factory.setPassword(httpClientAuthentiacationHandler
				.getDefaultPassword());
		factory.setAddress(address);
		try {
			factory.setServiceClass((Class<T>) Class.forName(serviceClass));
		} catch (ClassNotFoundException e) {
			throw new SurfilterException("没有找到类",e);
		}
		T ret = (T) factory.create();
		clientMap.put(clientId, ret);
		return ret;
	}

	public String getServiceClass() {
		return serviceClass;
	}

	public void setServiceClass(String serviceClass) {
		this.serviceClass = serviceClass;
	}

	public void setWsdlProvider(WsdlProvider wsdlProvider) {
		this.wsdlProvider = wsdlProvider;
	}

	public void setHttpClientAuthentiacationHandler(
			HttpClientAuthentiacationHandler httpClientAuthentiacationHandler) {
		this.httpClientAuthentiacationHandler = httpClientAuthentiacationHandler;
	};
}